﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Common.Class;
using ContractManager.Entity;
using ContractManager.Entity.Base;
using Speed.Data;

namespace ContractManager.BLL.Business
{
    public static class ClsWorkflow
    {
        /// <summary>
        ///     Get All Workflow
        /// </summary>
        /// <returns></returns>
        public static List<Workflow> SelectAll()
        {
            using (Database db = Sys.NewDb())
            {
                return Data.Base.Workflow.Select(db);
            }
        }

        /// <summary>
        ///     Get Lookup Workflow
        /// </summary>
        /// <returns></returns>
        public static DataTable GetLookupAll()
        {
            using (Database db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_Contracts_Search");
                return db.ExecuteDataTable(commandText);
            }
        }

        /// <summary>
        ///     Get Workflow by FormNo And Date
        /// </summary>
        /// <param name="formNo"></param>
        /// <param name="voucherCate"></param>
        /// <param name="fromDate"></param>
        /// <param name="toDate"></param>
        /// <returns></returns>
        public static DataTable GetAll(int formNo, int voucherCate, DateTime fromDate, DateTime toDate)
        {
            using (Database db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_Workflow_GetAll {0},{1},{2},{3},'{4}','{5}'",
                    ClsVariable.AsSysMyCoYearId,
                    formNo,
                    voucherCate,
                    ClsVariable.EmployeeId,
                    fromDate.ToString("dd/MMM/yyyy"),
                    toDate.ToString("dd/MMM/yyyy"));
                return db.ExecuteDataTable(commandText);
            }
        }


        /// <summary>
        ///     Get Workflow detail by WorkflowId
        /// </summary>
        /// <param name="workflowId"></param>
        /// <returns></returns>
        public static DataTable ListDetail(Int32 workflowId)
        {
            using (Database db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_WorkflowDetail_SelectByMaster {0}", workflowId);
                return db.ExecuteDataTable(commandText);
            }
        }

        /// <summary>
        ///     Add New Workflow
        /// </summary>
        /// <param name="capital"></param>
        public static void Insert(Workflow capital)
        {
            using (Database db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Insert<Workflow>(capital, EnumSaveMode.Requery);

                foreach (WorkflowDetails addItem in capital.ListAdd)
                {
                    addItem.WorkflowId = capital.WorkflowId;
                    db.Insert<WorkflowDetails>(addItem);
                }
                foreach (WorkflowDetails updateItem in capital.ListUpdate)
                {
                    updateItem.WorkflowId = capital.WorkflowId;
                    db.Update<WorkflowDetails>(updateItem);
                }
                foreach (WorkflowDetails delItem in capital.ListDelete)
                {
                    db.Delete<WorkflowDetails>(string.Format("WorkflowId={0}", delItem.WorkflowId));
                }

                db.Commit();
            }
        }

        /// <summary>
        ///     Edit Workflow
        /// </summary>
        /// <param name="capital"></param>
        public static void Update(Workflow capital)
        {
            using (Database db = Sys.NewDb())
            {
                db.BeginTransaction();
                db.Update<Workflow>(capital);
                foreach (WorkflowDetails addItem in capital.ListAdd)
                {
                    addItem.WorkflowId = capital.WorkflowId;
                    db.Insert<WorkflowDetails>(addItem);
                }
                foreach (WorkflowDetails updateItem in capital.ListUpdate)
                {
                    updateItem.WorkflowId = capital.WorkflowId;
                    db.Update<WorkflowDetails>(updateItem);
                }
                foreach (WorkflowDetails delItem in capital.ListDelete)
                {
                    db.Delete<WorkflowDetails>(string.Format("WorkflowId={0}", delItem.WorkflowId));
                }
                db.Commit();
            }
        }

        /// <summary>
        ///     Get Workflow Information
        /// </summary>
        /// <param name="workflowId"></param>
        /// <returns></returns>
        public static Workflow SelectByPk(Int32 workflowId)
        {
            using (Database db = Sys.NewDb())
            {
                return db.SelectSingle<Workflow>(string.Format("WorkflowId={0}", workflowId));
            }
        }

        /// <summary>
        ///     Delete Contract Information
        /// </summary>
        /// <param name="workflowId"></param>
        /// <returns></returns>
        public static int DeleteByPk(Int32 workflowId)
        {
            using (Database db = Sys.NewDb())
            {
                db.BeginTransaction();
                int ret = db.Delete<Workflow>(string.Format("WorkflowId={0}", workflowId));
                int rowCount = db.Delete<WorkflowDetails>(string.Format("WorkflowId={0}", workflowId));
                db.Commit();
                return ret;
            }
        }
    }
}